package com.niiwoo.civet.account.service.local.openAccount2.order;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import javax.annotation.PostConstruct;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class OpenAccountOrderHandler implements ApplicationContextAware {

    private Map<Class, OpenAccountOrderOperation> openAccountOrderOperationMap = new HashMap<>();

    private ApplicationContext ctx;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (applicationContext.getParent() == null) ctx = applicationContext;
    }

    @PostConstruct
    public void init() {
        Optional.ofNullable(ctx.getBeansOfType(OpenAccountOrderOperation.class)).ifPresent(m -> m.values().forEach(e -> openAccountOrderOperationMap.put(e.getType(), e)));
    }

    public <T> OpenAccountOrderOperation<T> get(Class<T> cls) {
        return openAccountOrderOperationMap.get(cls);
    }

}
